import numpy as np
import pandas as pd
import datetime

start = datetime.datetime.now()
realA = np.zeros(shape=(60,6800))
y_matA = pd.read_csv("y_mat.csv").to_numpy()[:,1:].astype(np.float32)
qt_matA = pd.read_csv("q_t_mat.csv").to_numpy()[:,1:].astype(np.float32)
sumQuestions1 = 0
sumQuestions2 = 0
correctNum1 = 0
errorNum1 = 0
correctNum2 = 0
errorNum2 = 0
nexam = y_matA.shape[0]
nitem = y_matA.shape[0]
ntopic = qt_matA.shape[1]

print("===========> VI Initialize Begins <===========")

T = 60
ts = 0
realATracked = np.zeros([T, nexam, ntopic])
allSelectedTopic = np.load('allSelectedTopic.npz')['arr_0']
N_S = np.load('N_S.npz')['arr_0']
N_F = np.load('N_F.npz')['arr_0']
tmpAns = np.zeros(shape=(nexam,4)) # 0:correct 1:error 2:sum 3:t1

while(ts < T):
    print("ts: "+str(ts))
    persons = []
    for i in range(nexam):
        flag = 0
        for j in range(ntopic):
            if allSelectedTopic[ts][i][j] != 0:
                flag = 1
                break
        if flag != 0:
            persons.append(i)
    # print(len(persons))
    n_s = N_S[ts]
    n_f = N_F[ts]
    for p in persons:
        # print("===========> Real data <===========")
        correctNum1 = tmpAns[p][0]
        errorNum1 = tmpAns[p][1]
        sumQuestions1 = tmpAns[p][2]
        noZero = 0
        # 更新tmpAns
        for q in range(ntopic):
            if allSelectedTopic[ts][p][q] != 0:
                noZero += 1
            if allSelectedTopic[ts][p][q] == 1:
                tmpAns[p][0] += 1
            elif allSelectedTopic[ts][p][q] == -1:
                tmpAns[p][1] += 1
        tmpAns[p][2] += noZero # 应该是加上allSelected中当前时刻的所有非0值

        correctNum2 = tmpAns[p][0]
        errorNum2 = tmpAns[p][1]
        sumQuestions2 = tmpAns[p][2]
        if sumQuestions1 == 0:
            realA[ts][p] = 0
        elif ts > 1:
            realATracked[ts, p, :] = (correctNum2 / sumQuestions2 - correctNum1 / sumQuestions1) / (ts - tmpAns[p][3]) * n_s[p,:] / nitem \
                            - (errorNum2 / sumQuestions2 - errorNum1 / sumQuestions1) / (ts - tmpAns[p][3]) * n_f[p,:] / nitem
            realA[ts][p] = sum(realATracked[ts][p]) / len(realATracked[ts][p])
        # print(realA[ts][p])
    for p in persons:
        tmpAns[p][3] = ts

    ts += 1

# print(realA.shape)
realA = pd.DataFrame(realA)
realA.to_csv('realA.csv')

end = datetime.datetime.now()
print(end)
print((end - start).seconds)
